import type { DailyFuture, Hourly } from '@/types/weather'

// 通过value.fxDate 来判断礼拜几
export function getDateLabel(dateIndex: string): string {
  const weekday = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  const data = new Date(dateIndex)
  const currentDate = new Date()

  if (data.getDate() === currentDate.getDate()) {
    return '今天'
  } else if (data.getDate() === currentDate.getDate() + 1) {
    return '明天'
  }
  return weekday[new Date(dateIndex).getDay()]
}

export function getMetricUnit(metric: string): string {
  switch (metric) {
    case '降水':
      return 'mm'
    case '风速':
      return 'km/h'
    case '湿度':
      return '%'
    case '云量':
      return '%'
    case '气压':
      return 'hPa'
    case '紫外线':
      return 'index'
    case '可见度':
      return 'km'
    case '体感温度':
      return '°C'
    case '概览':
      return '°C'
    case '温度':
      return '°C'
    default:
      return ''
  }
}
export function generateSeriesData(
  dateIndex: number,
  metricType: string,
  future: DailyFuture[] | null,
): number[] {
  // 如果没有数据，返回空数组
  if (!future || !future[dateIndex]) {
    return Array.from({ length: 24 }, () => 0)
  }

  // 获取当前日期的数据
  const currentData = future[dateIndex]

  // 根据不同的指标类型返回相应的数据
  switch (metricType) {
    case '降水':
      return Array.from({ length: 24 }, () => Number(currentData.precip) || 0)
    case '风速':
      return Array.from({ length: 24 }, () => Number(currentData.windSpeedDay) || 0)
    case '湿度':
      return Array.from({ length: 24 }, () => Number(currentData.humidity) || 0)
    case '云量':
      return Array.from({ length: 24 }, () => Number(currentData.cloud) || 0)
    case '气压':
      return Array.from({ length: 24 }, () => Number(currentData.pressure) || 0)
    case '紫外线':
      return Array.from({ length: 24 }, () => Number(currentData.uvIndex) || 0)
    case '可见度':
      return Array.from({ length: 24 }, () => Number(currentData.vis) || 0)
    case '体感温度':
      // 使用最高温度和最低温度的平均值作为体感温度
      const maxTemp = Number(currentData.tempMax) || 0
      const minTemp = Number(currentData.tempMin) || 0
      return Array.from({ length: 24 }, () => (maxTemp + minTemp) / 2)
    default:
      return Array.from({ length: 24 }, () => 0)
  }
}

export function generateHourlySeriesData(
  hourlyData: Hourly[] | null,
  metricType: string,
  selectedDateIndex: number,
): { hours: string[]; seriesData: number[] } {
  // 如果没有数据，返回空数组
  if (!hourlyData || hourlyData.length === 0) {
    return {
      hours: Array.from({ length: 24 }, (_, i) => `${i}:00`),
      seriesData: Array.from({ length: 24 }, () => 0),
    }
  }

  // 计算当前选中日期的24小时数据起始索引
  // 168小时数据，每24小时为一天
  const startIndex = selectedDateIndex * 24
  const endIndex = Math.min(startIndex + 24, hourlyData.length)

  // 提取小时数据和对应的指标数据
  const hours: string[] = []
  const seriesData: number[] = []

  for (let i = startIndex; i < endIndex; i++) {
    const hourlyItem = hourlyData[i]
    const date = new Date(hourlyItem.fxTime)
    hours.push(`${date.getHours()}:00`)

    // 根据不同的指标类型返回相应的数据
    switch (metricType) {
      case '降水':
        seriesData.push(Number(hourlyItem.precip) || 0)
        break
      case '风速':
        seriesData.push(Number(hourlyItem.windSpeed) || 0)
        break
      case '湿度':
        seriesData.push(Number(hourlyItem.humidity) || 0)
        break
      case '云量':
        seriesData.push(Number(hourlyItem.cloud) || 0)
        break
      case '气压':
        seriesData.push(Number(hourlyItem.pressure) || 0)
        break
      case '紫外线':
        // 小时级别没有紫外线数据，暂时设为0
        seriesData.push(0)
        break
      case '可见度':
        // 小时数据中没有可见度，暂时设为0
        seriesData.push(0)
        break
      case '体感温度':
        seriesData.push(Number(hourlyItem.temp) || 0)
        break
      case '概览':
        seriesData.push(Number(hourlyItem.temp))
        break
      default:
        seriesData.push(0)
    }
  }

  // 如果数据不足24小时，用0填充
  while (hours.length < 24) {
    hours.push(`${hours.length}:00`)
    seriesData.push(0)
  }
  return { hours, seriesData }
}

// 切换卡片时返回数据
export function CardChenage(metricType: string, item: DailyFuture) {
  switch (metricType) {
    case '降水':
      return {
        name: '降水',
        className: 'rain',
        unit: '毫米',
        // total: 50,
        data: item.precip || 0,
        percentage: getPercent(item.precip, 50),
      }
    case '风速':
      return {
        name: '风速',
        className: 'wind',
        unit: 'km/h',
        // total: 10,
        data: item.windSpeedDay || 0,
        percentage: getPercent(item.windSpeedDay, 10),
      }
    case '湿度':
      return {
        name: '湿度',
        className: 'humidity',
        unit: '%',
        // total: 100,
        data: item.humidity,
        percentage: getPercent(item.humidity, 100),
      }
    case '云量':
      return {
        name: '云量',
        className: 'cloud',
        unit: '%',
        // total: 100,
        data: item.cloud,
        percentage: getPercent(item.cloud, 100),
      }
    case '气压':
      return {
        name: '气压',
        className: 'pressure',
        unit: 'hPa',
        // total: 1000,
        data: item.pressure,
        percentage: getPercent(item.pressure, 1000),
      }
    case '体感温度':
      return {
        name: '体感温度',
        data: 0,
      }
    default:
      break
  }
}
// 计算长条百分比
export function getPercent(value: string, total: number): number {
  const num = Number(value)
  return (num / total) * 100
}
